package com.bicon.botu.mall.base.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.mall.base.entity.Channel;
import com.bicon.botu.mall.base.mapper.ChannelMapper;
import com.bicon.botu.mall.base.service.IChannelService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 频道 服务实现类
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@Service
@Transactional
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {
    /**
     * 增加频道
     *
     * @param channelTitle 频道名称
     * @param channelFlag  频道开关
     * @param channelType  频道类型：0-为你精选 1-猜你喜欢 2-新品首发 3-人气推荐
     * @return
     */
    @Override
    public BaseResult insert(String channelTitle, Integer channelFlag, Integer channelType) {
        boolean result;
        //判断频道是否已经存在
        Channel channel1 = new Channel().selectOne(new EntityWrapper<Channel>().eq("channel_type", channelType)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (channel1 != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        //新增频道实体
        Channel channel = new Channel();
        channel.setChannelType(channelType);
        channel.setChannelTitle(channelTitle);
        channel.setChannelFlag(channelFlag);
        channel.preInsert();
        result = channel.insert();
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改频道
     *
     * @param channelId    频道id
     * @param channelTitle 频道名称
     * @param channelFlag  频道开关
     * @return
     */
    @Override
    public BaseResult update(Integer channelId, String channelTitle, Integer channelFlag) {
        boolean result;
        Channel channel = new Channel().selectOne(new EntityWrapper<>().eq("id", channelId)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (null == channel) {
            return new BaseResult(Constants.RESPONSE_CODE_404, Constants.RESPONSE_MESSAGE_404);
        }
        channel.preUpdate();
        if (null != channelTitle) {
            channel.setChannelTitle(channelTitle);
        }
        if (null != channelFlag) {
            channel.setChannelFlag(channelFlag);
        }
        //更新频道表
        result = channel.updateById();
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 逻辑删除频道
     *
     * @param id 频道Id
     * @return
     */
    @Override
    public BaseResult deleteChannel(Integer id) {
        boolean result;
        //判断频道是否存在
        Channel channel = new Channel().selectOne(new EntityWrapper<Channel>().eq("id", id)
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        if (null == channel) {
            return new BaseResult(MallHomePageConstants.RESPONSE_CODE_404, MallHomePageConstants.RESPONSE_MESSAGE_404);
        }
        channel.setLogicDelete(MallHomePageConstants.DEL_LOGIC_DELETE);
        channel.preUpdate();
        result = channel.updateById();
        if (!result) {
            return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

}
